package org.jetlinks.community.mstn.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.hswebframework.web.api.crud.entity.QueryNoPagingOperation;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.api.crud.entity.TreeSupportEntity;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.crud.events.EntityBeforeDeleteEvent;
import org.hswebframework.web.crud.events.EntityModifyEvent;
import org.hswebframework.web.crud.service.ReactiveTreeSortEntityService;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.jetlinks.community.mstn.entity.AliasCategoryEntity;
import org.jetlinks.community.mstn.service.AliasCategoryService;
import org.jetlinks.community.mstn.service.DeviceAliasService;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 数字孪生分类
 */
@RestController
@RequestMapping("/alias/category")
@Authorize
@Resource(id = "alias-category", name = "数字孪生分类", group = "mstn")
@Tag(name = "数字孪生分类")
@AllArgsConstructor
public class AliasCategoryController implements ReactiveServiceCrudController<AliasCategoryEntity, String> {

    private final AliasCategoryService aliasCategoryService;

    @Override
    public ReactiveTreeSortEntityService<AliasCategoryEntity, String> getService() {
        return aliasCategoryService;
    }


    @GetMapping
    @QueryNoPagingOperation(summary = "获取全部分类")
    @Authorize(merge = false)
    @Operation(summary = "获取全部分类")
    public Flux<AliasCategoryEntity> getAllCategory(@Parameter(hidden = true) QueryParamEntity query) {
        return this
            .aliasCategoryService
            .createQuery()
            .setParam(query)
            .fetch();
    }

    @GetMapping("/_tree")
    @QueryNoPagingOperation(summary = "获取全部分类(树结构)")
    @Authorize(merge = false)
    @Operation(description = "获取全部分类(树结构)")
    public Flux<AliasCategoryEntity> getAllCategoryTree(@Parameter(hidden = true) QueryParamEntity query) {
        return this
            .aliasCategoryService
            .createQuery()
            .setParam(query)
            .fetch()
            .collectList()
            .flatMapMany(all -> Flux.fromIterable(TreeSupportEntity.list2tree(all, AliasCategoryEntity::setChildren)));
    }


    @PostMapping("/_tree")
    @QueryNoPagingOperation(summary = "获取全部分类(树结构)")
    @Authorize(merge = false)
    @Operation(description = "获取全部分类(树结构)")
    public Flux<AliasCategoryEntity> getAllCategoryTreeByQueryParam(@RequestBody Mono<QueryParamEntity> query) {
        return this
            .aliasCategoryService
            .query(query)
            .collectList()
            .flatMapMany(all -> Flux.fromIterable(TreeSupportEntity.list2tree(all, AliasCategoryEntity::setChildren)));
    }



}
